﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using ColorApplication.Helper;
using System.Configuration;

namespace ColorApplication.color
{
    public partial class Offset : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Checking whether Admin or Not. AdminID key is in web.config
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "" && Request.QueryString["id"] == ConfigurationManager.AppSettings["AdminID"].ToString())
            {
                Clear();
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        public void Clear()
        {
            // Clear Error And Success label Every Post Bak
            SuccesLabel.Text = "";
            ErrorLabel.Text = "";

        }

        protected void OffsetDetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record inserted successfully.";
                ErrorLabel.Text = "";
                OffsetDetailsView.ChangeMode(DetailsViewMode.Insert);
                OffsetGridView.DataBind();
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void OffsetDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {

            try
            {
                string Attribute1 = e.Values["Attribute1"].ToString();
                string Attribute2 = e.Values["Attribute2"].ToString();

                // This Function Check whether attributes are same or not
                if (Attribute1 != Attribute2)
                {
                    // This Function Check Record in Offset for Duplicate exist or Not
                    if (CheckAttributeExistinOffset(Attribute1, Attribute2))
                    {
                        ErrorLabel.Text = "Pair already exists. Duplicate record is not allowed.";
                        e.Cancel = true;
                        //Cancel And Show Error Message
                    }
                }
                else
                {
                    ErrorLabel.Text = "Both attribute must not be same.";
                    e.Cancel = true;
                    //Cancel And Show Error Message
                }

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        private bool CheckAttributeExistinOffset(string Attribute1, string Attribute2)
        {
            // This Function Check Record in Offset Table For Duplicate Pair Exist or Not
            // if Record Found Return true
            //else return false

            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection())
            {
                SqlParameter[] parameters = {
                                                new SqlParameter("@Attribute1", Attribute1),
                                                new SqlParameter("@Attribute2", Attribute2)
                                            };

                SqlCommand cmd = SQLHelper.GetCommand(con, "CheckAttributeExistinOffset", parameters);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                return true;
            }

            return false;
        }

        protected void OffsetDetailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record updated successfully.";
                ErrorLabel.Text = "";
                OffsetDetailsView.ChangeMode(DetailsViewMode.Insert);
                OffsetGridView.DataBind();
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }

        }

        protected void OffsetDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            try
            {
                string oldAttribute1 = e.OldValues["Attribute1"].ToString();
                string newAttribute1 = e.NewValues["Attribute1"].ToString();
                string oldAttribute2 = e.OldValues["Attribute2"].ToString();
                string newAttribute2 = e.NewValues["Attribute2"].ToString();

                if (oldAttribute1 != newAttribute1 || oldAttribute2 != newAttribute2)
                {
                    // This Function Check whether attributes are same or not
                    if (newAttribute1 != newAttribute2)
                    {
                        // This Function Check Record in Attribute for Dublicate Description name exist or Not
                        if (CheckAttributeExistinOffset(newAttribute1, newAttribute2) || CheckAttributeExistinOffset(newAttribute2, newAttribute1))
                        {
                            ErrorLabel.Text = "Pair already exists. Duplicate record is not allowed.";
                            e.Cancel = true;
                            //Cancel And Show Error Message
                        }
                    }
                    else
                    {
                        ErrorLabel.Text = "Both attribute must not be same.";
                        e.Cancel = true;
                        //Cancel And Show Error Message
                    }
                }

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void OffsetGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                // Change Deatils view Mode to edit
                OffsetDetailsView.ChangeMode(DetailsViewMode.Edit);
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void OffsetGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int OffsetID = Convert.ToInt32(e.Keys["Id"].ToString());

                // This Function Check Record in ColorsAttribute Table
                //if (CheckRecordColorsAttribute(AttributeId, "AttributeId") == true)
                //{
                //    ErrorLabel.Text = "This Arrtibute is used in colors attribute.Delete is not allowed.";
                //    e.Cancel = true;
                //    //Cancel And Show Error Message
                //}
                //else if (CheckAttributesInAttributesXAttributesfamilies(AttributeId) == true)  // This Function Check Record in Sub Color Table
                //{
                //    ErrorLabel.Text = "This Arrtibute is used in Attributes X Attributesfamilies.Delete is not allowed.";
                //    e.Cancel = true;
                //    //Cancel And Show Error Message
                //}

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }
    }
}